/***
 *                ---------
                   _ooOoo_
                  o8888888o
                  88" . "88
                  (| -_- |)
                  O  =  /O
               ____/`---'____
             .'  \|     |//  `.
            /  \|||  :  |||//
           /  _||||| -:- |||||-
           |   | \  -  /// |   |
           | _|  ''---/''  |   |
             .-__  `-`  ___/-. /
         ___`. .'  /--.--  `. . __
      ."" '<  `.____<|>_/___.'  >'"".
     | | :  `- `.;` _ /`;.`/ - ` : | |
        `-.   _ __ /__ _/   .-` /  /
======`-.____`-.________/___.-`____.-'======
                   `=---='
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
         佛祖保佑       永无BUG
***/

/****
 *  author: wanghe
 *  datetime:
 *
 * ***/

#include "dialog.h"
#include "ui_dialog.h"
#include <iostream>
#include <QSettings>

class TimeScaleDraw: public QwtScaleDraw
{
private:
    QString *xData;

public:
       TimeScaleDraw()
    {
        this->xData = xData;
    }

    virtual QwtText label( double v ) const
    {
           QString text;
           return text.setNum((uint)v);

     }

};


using namespace std;

   Dialog::Dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::   Dialog), colorview(NULL)
{
    ui->setupUi(this);

    this->d_legend_color_temp = NULL;


    Qt::WindowFlags flags=Qt::Dialog;

    flags |=Qt::WindowMinimizeButtonHint;
    flags |= Qt::WindowMaximizeButtonHint;
    flags |= Qt::WindowCloseButtonHint;
    setWindowFlags(flags);

    this->d_barChartItem_ftl = NULL;
    this->d_legend_ftl = NULL;
    this->d_barChartItem_other = NULL;
    this->d_barChartItem = NULL;

    this->d_legend_other = NULL;
    this->d_barChartItem_spl = NULL;
    this->d_legend_spl = NULL;

    this->d_legend_color_temp = NULL;
    this->d_curve_color_temp = NULL;
    this->d_curve_std_color_temp = NULL;

    this->d_legend_temp = NULL;
    this->d_curve_temp_c = NULL;
    this->d_curve_temp_f = NULL;

    this->d_legend_ftl = NULL;
    this->d_curve_ftl_white = NULL;
    this->d_curve_ftl_red = NULL;
    this->d_curve_ftl_green = NULL;
    this->d_curve_ftl_blue = NULL;

    this->splview = NULL;

    setWindowTitle(tr("QMC²S放映质量与成本控制系统"));
    resize(450, 500);



     int numSamples, numBars;

     numSamples = 6;
     numBars = 8;

     QVector< QVector<double> > series1;
     for ( int i = 0; i < numSamples; i++ )
     {
         QVector<double> values;
         for ( int j = 0; j < numBars; j++ )
             values += ( 2 + qrand() % 80 );

         series1 += values;
     }

     DrawBarChartPlot(ui->qwtPlot_5, tr("上层"),  series1, numBars);
     DrawBarChartPlot(ui->qwtPlot_6, tr("中层"), series1, numBars);
     DrawBarChartPlot(ui->qwtPlot_7, tr("下层"), series1, numBars);

     QPalette pal;
     pal = ui->plainTextEdit->palette();
     pal.setColor(QPalette::Base, QColor(0,0,0));//改变背景
     pal.setColor(QPalette::Text, QColor(0,255,0));
     // 汇总 初始化tableWidget
     initDB();


     //初始化ui界面
     initUI();

     // 初始始翻译
     installTranslator();
     connect( ui->lc_Slider, SIGNAL( valueChanged( double ) ), SLOT( setLcNum(double) ) );
     connect( ui->rc_Slider, SIGNAL( valueChanged( double ) ), SLOT( setRcNum(double) ) );
     connect( ui->cc_Slider, SIGNAL( valueChanged( double ) ), SLOT( setCcNum(double) ) );
     connect( ui->hb_Slider, SIGNAL( valueChanged( double ) ), SLOT( setHbNum(double) ) );
     connect( ui->la_Slider, SIGNAL( valueChanged( double ) ), SLOT( setLaNum(double) ) );
     connect( ui->ra_Slider, SIGNAL( valueChanged( double ) ), SLOT( setRaNum(double) ) );
     connect( ui->temp_Slider, SIGNAL( valueChanged( double ) ), SLOT( setTempNum(double) ) );
     connect( ui->cct_Slider, SIGNAL( valueChanged( double ) ), SLOT( setCctNum(double) ) );

     connect( ui->white_ftl_Slider, SIGNAL( valueChanged( double ) ), SLOT( setWhiteFtl(double) ) );
     connect( ui->red_ftl_Slider, SIGNAL( valueChanged( double ) ), SLOT( setRedFtl(double) ) );
     connect( ui->green_ftl_Slider, SIGNAL( valueChanged( double ) ), SLOT( setBlueFtl(double) ) );
     connect( ui->blue_ftl_Slider, SIGNAL( valueChanged( double ) ), SLOT( setGreenFtl(double) ) );


}

void Dialog::resizeEvent(QResizeEvent *e)
{
   QDialog::resizeEvent(e);
   QResizeEvent e1(ui->SplViewWidget->size(), ui->SplViewWidget->size());
   splview->setFixedSize(ui->SplViewWidget->size());
   splview->resizeEvent(&e1);
   QResizeEvent e2(ui->colorViewWidget->size(), ui->colorViewWidget->size());
   colorview->setFixedSize(ui->colorViewWidget->size());
   colorview->resizeEvent(&e2);
}

void Dialog::setWhiteFtl(double v)
{
    QString text;
    text.setNum(v);
    ui->whlineEdit_2->setText(text);
}

void Dialog::setRedFtl(double v)
{
    QString text;
    text.setNum(v);
    ui->red_ftl_lineEdit->setText(text);
 }

void Dialog::setBlueFtl(double v)
{
    QString text;
    text.setNum(v);
    ui->blue_ftl_lineEdit->setText(text);
 }




void Dialog::setGreenFtl(double v)
{
    QString text;
    text.setNum(v);
    ui->green_ftl_lineEdit->setText(text);
 }

void Dialog::setLcNum(double v)
{
    QString text;
    text.setNum( v);
    ui->lclineEdit_2->setText(text);
}

void Dialog::setRcNum(double v)
{
    QString text;
    text.setNum( v);
    ui->rclineEdit_2->setText(text);
}

void Dialog::setCcNum(double v)
{
    QString text;
    text.setNum( v);
    ui->cclineEdit_2->setText(text);
}

void Dialog::setHbNum(double v)
{
    QString text;
    text.setNum( v);
    ui->hblineEdit_2->setText(text);
}

void Dialog::setLaNum(double v)
{
    QString text;
    text.setNum(v);
    ui->lrlineEdit_2->setText(text);
}

void Dialog::setRaNum(double v)
{
    QString text;
    text.setNum(v);
    ui->rrlineEdit_2->setText(text);
}

void Dialog::setTempNum(double v)
{
    QString text;
    text.setNum(v);
    ui->temp_lineEdit_2->setText(text);
}

void Dialog::setCctNum(double v)
{
    QString text;
    text.setNum(v);
    ui->cctlineEdit_2->setText(text);
}

void Dialog::refreshTotal()
{
ui->TotaltableView->setModel(model);
}

void Dialog::reloadCombo()
{
    QStringList list;
    QModelIndex indextemp;
    QString sql= "select * from tb_dev_corrected_value_conf;";

    model_conf->setQuery(sql, db);
    for(int i = 0; i<model_conf->rowCount(); i++)
    {
        indextemp = model_conf->index(i, 2);
        QString data = model_conf->data(indextemp).toString();
        list<<data;
    }


    ui->qry_select_cinema_hall_comboBox->clear();
    ui->standard_select_cinema_hall_comboBox_2->clear();
    ui->select_cinema_comboBox->clear();
    ui->select_cinema_comboBox_3->clear();
    ui->hallcomboBox->clear();

    ui->qry_select_cinema_hall_comboBox->addItems(list);
    ui->qry_select_cinema_hall_comboBox->addItem(QObject::tr("所有影厅"));
    ui->standard_select_cinema_hall_comboBox_2->addItems(list);
    ui->select_cinema_comboBox->addItems(list);
    ui->select_cinema_comboBox_3->addItems(list);
    ui->hallcomboBox->addItems(list);

    list.clear();
}

void Dialog::initUI()
{

    QString user = model_user->data(model_user->index(0, 1)).toString();
    QString passwd = model_user->data(model_user->index(0, 2)).toString();
    QString email = model_user->data(model_user->index(0, 3)).toString();
    QString tel = model_user->data(model_user->index(0, 4)).toString();
    QString cinema = model_user->data(model_user->index(0, 5)).toString();

    ui->user_lineEdit->setText(user);
    ui->passwd_lineEdit->setText(passwd);
    ui->email_lineEdit->setText(email);
    ui->tel_lineEdit->setText(tel);

    ui->DetailtableView->setModel(model_detail_view);

    QStringList list;
    QModelIndex indextemp;
    for(int i = 0; i<model_conf->rowCount(); i++)
    {
        indextemp = model_conf->index(i, 2);
        QString data = model_conf->data(indextemp).toString();
        list<<data;
    }
    ui->qry_select_cinema_hall_comboBox->addItems(list);
    ui->qry_select_cinema_hall_comboBox->addItem(tr("所有影厅"));
    ui->standard_select_cinema_hall_comboBox_2->addItems(list);
    ui->select_cinema_comboBox->addItems(list);
    ui->select_cinema_comboBox_3->addItems(list);
    ui->hallcomboBox->addItems(list);
    ui->select_cinema_comboBox_4->addItems(list);
    list.clear();

    ui->qry_start_dateTimeEdit->setDateTime(QDateTime::currentDateTime());
    ui->qry_end_dateTimeEdit_2->setDateTime(QDateTime::currentDateTime());
    ui->lc_Slider->setValue(20.0);
    initStdTab();
    DrawColorQwtPlot(ui->ColorTempqwtPlot, ui->select_cinema_comboBox->currentText() );
    DrawTempQwtPlot(ui->TempqwtPlot, ui->select_cinema_comboBox->currentText());
    DrawFtlQwtPlot(ui->FtlqwtPlot,  ui->select_cinema_comboBox->currentText());
    DrawSplQwtPlot(ui->select_cinema_comboBox->currentText());
    DrawColorAreaXy(ui->select_cinema_comboBox->currentText());

    QPolygonF points;

    points << QPointF( 0.0, 4.4 ) << QPointF(0.12, 1.1)<< QPointF( 1.0, 3.0 )
        << QPointF( 2.0, 4.5 ) << QPointF( 3.0, 6.8 )
        << QPointF( 4.0, 7.9 ) << QPointF( 5.0, 7.1 );

   InitUserTableView();
   loadTree();
   init_email();
   init_about();
   QSettings *settings = new QSettings("config.ini", QSettings::IniFormat);
   QString language = settings->value("language").toString();
   if(language == "en")
   {
       ui->language_comboBox->setCurrentIndex(0);
   }
   else
   {
       ui->language_comboBox->setCurrentIndex(1);
   }
   delete settings;



}

void Dialog::InitUserTableView()
{
      model_user->setHeaderData(0, Qt::Horizontal, tr("编号"));
      model_user->setHeaderData(1, Qt::Horizontal, tr("用户名"));
      model_user->setHeaderData(2, Qt::Horizontal, tr("密码"));
      model_user->setHeaderData(3, Qt::Horizontal, tr("邮箱"));
      model_user->setHeaderData(4, Qt::Horizontal, tr("电话"));
      model_user->setHeaderData(5, Qt::Horizontal, tr("影城"));
      ui->usertableView->setModel(model_user);

      ui->usertableView->setSelectionBehavior(QAbstractItemView::SelectRows);
      ui->usertableView->setSelectionMode(QAbstractItemView::SingleSelection);
}

void Dialog::initStdTab()
{


    QString lc = model_std->data(model_std->index(0, 1)).toString();
    QString rc = model_std->data(model_std->index(0, 2)).toString();
    QString cc = model_std->data(model_std->index(0, 3)).toString();
    QString hb = model_std->data(model_std->index(0, 4)).toString();
    QString la = model_std->data(model_std->index(0, 5)).toString();
    QString ra = model_std->data(model_std->index(0, 6)).toString();
    QString white_ftl = model_std->data(model_std->index(0, 7)).toString();
    QString red_ftl = model_std->data(model_std->index(0, 8)).toString();
    QString green_ftl = model_std->data(model_std->index(0, 9)).toString();
    QString blue_ftl = model_std->data(model_std->index(0, 10)).toString();
    QString temp = model_std->data(model_std->index(0, 11)).toString();
    QString cct = model_std->data(model_std->index(0, 12)).toString();

    ui->lclineEdit_2->setText(lc);
    ui->rclineEdit_2->setText(rc);
    ui->cclineEdit_2->setText(cc);
    ui->hblineEdit_2->setText(hb);
    ui->lrlineEdit_2->setText(la);
    ui->rrlineEdit_2->setText(ra);
    ui->whlineEdit_2->setText(white_ftl);
    ui->red_ftl_lineEdit->setText(red_ftl);
    ui->green_ftl_lineEdit->setText(green_ftl);
    ui->blue_ftl_lineEdit->setText(blue_ftl);
    ui->temp_lineEdit_2->setText(temp);
    ui->cctlineEdit_2->setText(cct);

    ui->lc_Slider->setValue(lc.toFloat());
    ui->rc_Slider->setValue(rc.toFloat());
    ui->cc_Slider->setValue(cc.toFloat());
    ui->hb_Slider->setValue(hb.toFloat());
    ui->la_Slider->setValue(la.toFloat());
    ui->ra_Slider->setValue(ra.toFloat());
    ui->red_ftl_Slider->setValue(red_ftl.toFloat());
    ui->blue_ftl_Slider->setValue(blue_ftl.toFloat());
    ui->green_ftl_Slider->setValue(green_ftl.toFloat());
    ui->cct_Slider->setValue(cct.toDouble());
    ui->temp_Slider->setValue(temp.toDouble());


 }

void Dialog::setNum(QLineEdit *lineEdit,  double v)
{
    QString text;
    text.setNum( v);
    lineEdit->setText(text);
}

void Dialog::initDB()
{
    db = QSqlDatabase::addDatabase("QMYSQL","dev_monitor");
    QSqlError sqlerr = db.lastError();
    if (sqlerr.type() != QSqlError::NoError)
    {
        printf("DATABASE INIT ERROR: %s\n", sqlerr.text().toStdString().c_str());
        QMessageBox::information(this, tr("Db Error"), tr("Failed to connect mysql"));
        exit(-1);
        return ;
    }
    db.setHostName("localhost");
    db.setDatabaseName("dev_monitor");
    db.setUserName("root");
    db.setPassword("123qwe");
    if (!db.open())
    {
        printf("DATABASE CONNECTION ERROR: %s\n", db.lastError().text().toStdString().c_str());
        QMessageBox::information(this, tr(" db open Error"), tr("Failed to open database."));
        exit(-1);
        return ;
    }

    model = new QSqlQueryModel(this);
    QString sql = "select * from tb_dev_test_results";

    model->setQuery(sql, db);
    model->setHeaderData(0, Qt::Horizontal, tr("编号"));
    model->setHeaderData(1, Qt::Horizontal, tr("影城"));
    model->setHeaderData(2, Qt::Horizontal, tr("影厅"));
    model->setHeaderData(3, Qt::Horizontal, tr("设备IP"));
    model->setHeaderData(4, Qt::Horizontal, tr("左声道(dB)"));
    model->setHeaderData(5, Qt::Horizontal, tr("右声道(dB)"));
    model->setHeaderData(6, Qt::Horizontal, tr("中置(dB)"));
    model->setHeaderData(7, Qt::Horizontal, tr("重低音(dB)"));
    model->setHeaderData(8, Qt::Horizontal, tr("左环(dB)"));
    model->setHeaderData(9, Qt::Horizontal, tr("右环(dB)"));
    model->setHeaderData(10, Qt::Horizontal, tr("色温(dB)"));
    model->setHeaderData(11, Qt::Horizontal, tr("白光X"));
    model->setHeaderData(12, Qt::Horizontal, tr("白光Y"));
    model->setHeaderData(13, Qt::Horizontal, tr("红光X"));
    model->setHeaderData(14, Qt::Horizontal, tr("红光Y"));
    model->setHeaderData(15, Qt::Horizontal, tr("绿光X"));
    model->setHeaderData(16, Qt::Horizontal, tr("绿光Y"));
    model->setHeaderData(17, Qt::Horizontal, tr("蓝光X"));
    model->setHeaderData(18, Qt::Horizontal, tr("蓝光Y"));
    model->setHeaderData(19, Qt::Horizontal, tr("白光亮度(fL)"));
    model->setHeaderData(20, Qt::Horizontal, tr("红光色度"));
    model->setHeaderData(21, Qt::Horizontal, tr("绿光色度"));
    model->setHeaderData(22, Qt::Horizontal, tr("蓝光色度"));
    model->setHeaderData(23, Qt::Horizontal, tr("温度摄氏(℃)"));
    model->setHeaderData(24, Qt::Horizontal, tr("温度华氏(°F)"));
    model->setHeaderData(25, Qt::Horizontal, tr("开始测试时间"));
    model->setHeaderData(26, Qt::Horizontal, tr("结束测试时间"));


    model_conf = new QSqlQueryModel(this);
    QString sql2 = "select * from tb_dev_corrected_value_conf";
    model_conf->setQuery(sql2, db);

    sql = QObject::tr("select * from tb_dev_test_results");
    model->setQuery(sql, db);
    ui->TotaltableView->setModel(model);
    QString cinema_hall = "1号厅";
    sql = QObject::tr("select * from tb_dev_test_results where cinema_hall = '%1'").arg(cinema_hall);
    model_detail = new QSqlQueryModel();
    model_detail->setQuery(sql, db);

    model_std  = new QSqlQueryModel(this);
    model_std->setQuery("select * from test_std", db);

    model_detail_view = new QSqlQueryModel(this);
    model_detail_view->setQuery("select * from tb_dev_test_results where cinema_hall = '1号厅' order by id desc limit 5", db);

    model_std = new QSqlQueryModel(this);
    model_std->setQuery("select * from std_test where cinema_hall = '1号厅'", db);

    model_3d = new QSqlQueryModel(this);
    model_3d->setQuery("select * from 3d", db);

    model_user = new QSqlQueryModel(this);
    model_user->setQuery("select * from user", db);

    model_offset = new QSqlQueryModel(this);
    model_offset->setQuery("select * from offset_test", db);

    model_email = new QSqlQueryModel(this);
    model_email->setQuery("selct * from email_server", db);

    ui->DetailtableView->setModel(model_detail_view);


//   ui->DetailtableView->gridStyle(Qt::PenStyle);
}

   Dialog::~Dialog()
{
    delete ui;
}

void  Dialog::InitPlots()
{


}



void    Dialog::DrawBarChartPlot(QwtPlot *plot, QString title, QVector< QVector<double> > series, int numBars)
{
#if 1
    // 其他选项


    plot->setCanvasBackground(Qt::white);
    plot->setAxisScale(QwtPlot::xBottom, 0, 100);
    plot->setAxisScale(QwtPlot::yLeft, 0, 1000);
//    ui->SplQwtPlot->setAxisScaleDraw(QwtPlot::xBottom, new TimeScaleDraw());

    plot->setAutoFillBackground( true );
    plot->setPalette( Qt::white );
    plot->canvas()->setPalette( QColor( "LemonChiffon" ) );

    plot->setTitle( title );

    plot->setAxisTitle( QwtPlot::yLeft, tr("分贝") );
    plot->setAxisTitle( QwtPlot::xBottom, tr("时间") );

    d_barChartItem = new QwtPlotMultiBarChart( "Bar Chart " );
    d_barChartItem->setLayoutPolicy( QwtPlotMultiBarChart::AutoAdjustSamples );
    d_barChartItem->setSpacing( 20 );
    d_barChartItem->setMargin( 3 );

    d_barChartItem->attach( plot );

    d_legend = new QwtLegend();

    plot->insertLegend( d_legend );


    static const char *colors[] = { "DarkOrchid", "SteelBlue", "Gold", "red", "#eeffee", "#12abcc", "#fecd12", "#8abd43"};

//    const int numSamples = 4;
//    const int numBars = sizeof( colors ) / sizeof( colors[0] );

    QList<QwtText> titles;
    titles<<QString(tr("左声道")) << QString(tr("右声道")) <<QString(tr("中置"))
         << QString(tr("重低音"))<<QString(tr("左环"))<<QString(tr("右环"))<<QString(tr("左后")) <<QString(tr("右后"));


    d_barChartItem->setBarTitles( titles );
    d_barChartItem->setLegendIconSize( QSize( 10, 14 ) );

    for ( int i = 0; i < numBars; i++ )
    {
        QwtColumnSymbol *symbol = new QwtColumnSymbol( QwtColumnSymbol::Box );
        symbol->setLineWidth( 10 );
        symbol->setFrameStyle( QwtColumnSymbol::Raised );
        symbol->setPalette( QPalette( colors[i] ) );

        d_barChartItem->setSymbol( i, symbol );
    }

    d_barChartItem->setSamples( series );

    QwtPlot::Axis axis1, axis2;

    int orientation =0;

    if ( orientation == 0 )
    {
        axis1 = QwtPlot::xBottom;
        axis2 = QwtPlot::yLeft;

        d_barChartItem->setOrientation( Qt::Vertical );
    }
    else
    {
        axis1 = QwtPlot::yLeft;
        axis2 = QwtPlot::xBottom;

        d_barChartItem->setOrientation( Qt::Horizontal );
    }

    plot->setAxisScale( axis1, 0, d_barChartItem->dataSize() - 1, 1.0 );
    plot->setAxisAutoScale( axis2 );


    QwtScaleDraw *scaleDraw1 = plot->axisScaleDraw( axis1 );
    scaleDraw1->enableComponent( QwtScaleDraw::Backbone, false );
    scaleDraw1->enableComponent( QwtScaleDraw::Ticks, false );

    QwtScaleDraw *scaleDraw2 = ui->qwtPlot_5->axisScaleDraw( axis2 );
    scaleDraw2->enableComponent( QwtScaleDraw::Backbone, true );
    scaleDraw2->enableComponent( QwtScaleDraw::Ticks, true );

    plot->plotLayout()->setAlignCanvasToScale( axis1, true );
    plot->plotLayout()->setAlignCanvasToScale( axis2, false );

    plot->plotLayout()->setCanvasMargin( 0 );
    plot->updateCanvasMargins();

     plot->replot();
    plot->setAutoReplot( true );

#endif

}

void Dialog::refresh_model()
{
    QString sql = tr("select * from tb_dev_test_results ");
    model->setQuery(sql, db);

}

void Dialog::DrawColorQwtPlot(QwtPlot *plot, QString cinema_hall)
{
    if(d_legend_color_temp != NULL && d_curve_color_temp != NULL && d_curve_std_color_temp != NULL )
    {
        delete d_legend_color_temp;
        delete d_curve_color_temp;
        delete d_curve_std_color_temp;
    }

    QString sql = tr("select cct stime from tb_dev_test_results where cinema_hall like '%1' order by id desc limit 5;").arg(cinema_hall);
    model_detail->setQuery(sql, db);
    if(model_detail->lastError().isValid())
    {
        cout<<model->lastError().text().toStdString()<<endl;
        QMessageBox::information(this, "error", model_detail->lastError().text());
        return;
    }
     QPolygonF points;
     QPolygonF points_std;
     QModelIndex index;
     double xData[5] = {0};
     double yData[5] = {0};
     double xData_std[5] = {0}, yData_std[5] = {0};

     for(int i = 0; i<5; i++)
    {
        index = model_detail->index(i, 0);
        yData[4-i] = model_detail->data(index).toDouble();
        cout<<yData[4-i]<<endl;
        index = model_detail->index(i, 1);
        xData[i] = (double)i;

     }

    for(int i = 0; i<5; i++)
    {
          points<<QPointF(xData[i], yData[i]);
    }

   sql = QObject::tr("select cct from std_test where cinema_hall = '%1'; ").arg(cinema_hall);
   model_std->setQuery(sql, db);

   for(int i = 0; i<5; i++)
   {
       index = model_std->index(0, 0);
       yData_std[4-i] = model_std->data(index).toDouble();
       cout<<"fffff="<<yData_std[4-i]<<endl;
       index = model_std->index(i, 1);
       xData_std[i] = (double)i;
   }


    for(int i = 0; i<5; i++)
    {
        points_std << QPointF(xData_std[i], yData_std[i]);
        cout<<xData_std[i]<<","<<yData_std[i]<<endl;
    }

     plot->setTitle( tr("色温") );
     plot->setCanvasBackground( Qt::white );
     plot->setAxisScale( QwtPlot::yLeft, 0.0, 100000.0 );
     plot->setAxisScale(QwtPlot::xBottom, 0.0, 5.0);
     plot->setAxisScaleDraw( QwtPlot::xBottom, new TimeScaleDraw());
     plot->setAxisLabelRotation( QwtPlot::xBottom, -0.0 );
     plot->setAxisLabelAlignment( QwtPlot::xBottom, Qt::AlignLeft | Qt::AlignBottom );
     d_legend_color_temp = new QwtLegend();
     plot->insertLegend( d_legend_color_temp );

    d_grid_color_temp = new QwtPlotGrid();
    d_grid_color_temp->attach( plot );

    d_curve_color_temp = new QwtPlotCurve();
    d_curve_color_temp->setTitle( tr("实际测量值") );
    d_curve_color_temp->setPen( Qt::blue, 4 ),
    d_curve_color_temp->setRenderHint( QwtPlotItem::RenderAntialiased, true );

    QwtSymbol *symbol = new QwtSymbol( QwtSymbol::Ellipse,
        QBrush( Qt::yellow ), QPen( Qt::red, 2 ), QSize( 8, 8 ) );
    d_curve_color_temp->setSymbol( symbol );
    d_curve_color_temp->setSamples(points);
    d_curve_color_temp->attach(plot);

    d_curve_std_color_temp = new QwtPlotCurve();
    d_curve_std_color_temp->setTitle( tr("标准值 ") );
    d_curve_std_color_temp->setPen( Qt::red, 2 );
    d_curve_std_color_temp->setRenderHint( QwtPlotItem::RenderAntialiased, true );

    d_curve_std_color_temp->attach( plot );
    QwtSymbol *symbol2 = new QwtSymbol( QwtSymbol::Ellipse,
        QBrush( Qt::yellow ), QPen( Qt::red, 2 ), QSize( 8, 8 ) );
    d_curve_std_color_temp->setSymbol(symbol2);

    d_curve_std_color_temp->setSamples( points_std );

    d_curve_std_color_temp->attach( plot );
    ui->ColorTempqwtPlot->replot();

}

void Dialog::DrawTempQwtPlot(QwtPlot *plot, QString cinema_hall)
{
    if(d_legend_temp != NULL && d_curve_temp_c != NULL && d_curve_temp_f != NULL )
    {
        delete d_legend_temp;
        delete d_curve_temp_c;
        delete d_curve_temp_f;
    }

    QString sql = tr("select temper_c, temper_f, stime from tb_dev_test_results where cinema_hall like '%1' order by id desc limit 5;").arg(cinema_hall);
     model_detail->setQuery(sql, db);
    if(model_detail->lastError().isValid())
    {
        cout<<model->lastError().text().toStdString()<<endl;
        QMessageBox::information(this, "error", model_detail->lastError().text());
        return;
    }
     QPolygonF points;
     QPolygonF points_f;
     QModelIndex index;
     double xData[5] = {0};
     double yData[5] = {0};
     double xData_f[5] = {0}, yData_f[5] = {0};

     for(int i = 0; i<5; i++)
    {
        index = model_detail->index(i, 0);
        yData[4-i] = model_detail->data(index).toDouble();
        index = model_detail->index(i, 1);
        xData[i] = (double)i;

     }

    for(int i = 0; i<5; i++)
    {
          points<<QPointF(xData[i], yData[i]);
    }



   for(int i = 0; i<5; i++)
   {
       index = model_detail->index(i, 1);
       yData_f[4-i] = model_detail->data(index).toDouble();
       cout<<"fffff="<<yData_f[4-i]<<endl;
       index = model_detail->index(i, 1);
       xData_f[i] = (double)i;
   }


    for(int i = 0; i<5; i++)
    {
        points_f<< QPointF(xData_f[i], yData_f[i]);
        cout<<xData_f[i]<<","<<yData_f[i]<<endl;
    }

     plot->setTitle( tr("温度") );
     plot->setCanvasBackground( Qt::white );
     plot->setAxisScale( QwtPlot::yLeft, 0.0, 100.0 );
     plot->setAxisScale(QwtPlot::xBottom, 0.0, 5.0);
     plot->setAxisScaleDraw( QwtPlot::xBottom, new TimeScaleDraw());
     plot->setAxisLabelRotation( QwtPlot::xBottom, -0.0 );
     plot->setAxisLabelAlignment( QwtPlot::xBottom, Qt::AlignLeft | Qt::AlignBottom );
     d_legend_temp = new QwtLegend();
     plot->insertLegend( d_legend_temp );

    d_grid_temp = new QwtPlotGrid();
    d_grid_temp->attach( plot );

    d_curve_temp_c = new QwtPlotCurve();
    d_curve_temp_c->setTitle( tr("摄氏") );
    d_curve_temp_c->setPen( Qt::blue, 4 ),
    d_curve_temp_c->setRenderHint( QwtPlotItem::RenderAntialiased, true );

    QwtSymbol *symbol = new QwtSymbol( QwtSymbol::Ellipse,
        QBrush( Qt::yellow ), QPen( Qt::red, 2 ), QSize( 8, 8 ) );
    d_curve_temp_c->setSymbol( symbol );
    d_curve_temp_c->setSamples(points);
    d_curve_temp_c->attach(plot);

    d_curve_temp_f = new QwtPlotCurve();
    d_curve_temp_f->setTitle( tr("华氏") );
    d_curve_temp_f->setPen( Qt::red, 2 );
    d_curve_temp_f->setRenderHint( QwtPlotItem::RenderAntialiased, true );

    d_curve_temp_f->attach( plot );
    QwtSymbol *symbol2 = new QwtSymbol( QwtSymbol::Ellipse,
        QBrush( Qt::yellow ), QPen( Qt::red, 2 ), QSize( 8, 8 ) );
    d_curve_temp_f->setSymbol(symbol2);

    d_curve_temp_f->setSamples( points_f );

    d_curve_temp_f->attach( plot );
    plot->replot();
}


void Dialog::DrawFtlQwtPlot(QwtPlot *plot, QString cinema_hall)
{
    if(d_legend_ftl != NULL
            && d_curve_ftl_white != NULL
            && d_curve_ftl_red != NULL
            && d_curve_ftl_green != NULL
            && d_curve_ftl_blue != NULL
            )
    {
        delete d_legend_temp;
        delete d_curve_temp_c;
        delete d_curve_temp_f;
    }

    QString sql = tr("select white_ftl, red_ftl, green_ftl, blue_ftl, stime from tb_dev_test_results where cinema_hall like '%1' order by id desc limit 5;").arg(cinema_hall);
     model_detail->setQuery(sql, db);
    if(model_detail->lastError().isValid())
    {
        cout<<model->lastError().text().toStdString()<<endl;
        QMessageBox::information(this, "error", model_detail->lastError().text());
        return;
    }
     QPolygonF points_w, points_r, points_g, points_b;
     QPolygonF points_f;
     QModelIndex index_w, index_r, index_g, index_b;

     double xData_w[5] = {0}, yData_w[5] = {0};
     double xData_r[5] = {0}, yData_r[5] = {0};
     double xData_g[5] = {0}, yData_g[5] = {0};
     double xData_b[5] = {0}, yData_b[5] = {0};

     for(int i = 0; i<5; i++)
    {
            index_w = model_detail->index(i, 0);
            index_r = model_detail->index(i, 1);
            index_b = model_detail->index(i, 2);
            index_g = model_detail->index(i, 3);

            yData_w[4-i] = model_detail->data(index_w).toDouble();
            xData_w[i] = (double)i;

            yData_r[4-i] = model_detail->data(index_r).toDouble();
            xData_r[i] = (double)i;

            yData_g[4-i] = model_detail->data(index_g).toDouble();
            xData_g[i] = (double)i;

            yData_b[4-i] = model_detail->data(index_b).toDouble();
            xData_b[i] = (double)i;

     }

    for(int i = 0; i<5; i++)
    {
          points_w<<QPointF(xData_w[i], yData_w[i]);
          points_r<<QPointF(xData_r[i], yData_r[i]);
          points_g<<QPointF(xData_g[i], yData_g[i]);
          points_b<<QPointF(xData_b[i], yData_b[i]);

    }




     plot->setTitle( tr("亮度") );
     plot->setCanvasBackground( Qt::gray );
     plot->setAxisScale( QwtPlot::yLeft, 0.0, 100.0 );
     plot->setAxisScale(QwtPlot::xBottom, 0.0, 5.0);
     plot->setAxisScaleDraw( QwtPlot::xBottom, new TimeScaleDraw());
     plot->setAxisLabelRotation( QwtPlot::xBottom, -0.0 );
     plot->setAxisLabelAlignment( QwtPlot::xBottom, Qt::AlignLeft | Qt::AlignBottom );
     d_legend_ftl = new QwtLegend();
     plot->insertLegend( d_legend_ftl );

    d_grid_ftl = new QwtPlotGrid();
    d_grid_ftl->attach( plot );

    d_curve_ftl_white = new QwtPlotCurve();
    d_curve_ftl_white->setTitle( tr("白光亮度") );
    d_curve_ftl_white->setPen( Qt::yellow, 2 ),
    d_curve_ftl_white->setRenderHint( QwtPlotItem::RenderAntialiased, true );

    QwtSymbol *symbol = new QwtSymbol( QwtSymbol::Ellipse,
        QBrush( Qt::yellow ), QPen( Qt::red, 2 ), QSize( 8, 8 ) );
    d_curve_ftl_white->setSymbol( symbol );
    d_curve_ftl_white->setSamples(points_w);
    d_curve_ftl_white->attach(plot);

    d_curve_ftl_red = new QwtPlotCurve();
    d_curve_ftl_red->setTitle( tr("红光亮度 ") );
    d_curve_ftl_red->setPen( Qt::red, 2 );
    d_curve_ftl_red->setRenderHint( QwtPlotItem::RenderAntialiased, true );

    d_curve_ftl_red->attach( plot );
    QwtSymbol *symbol2 = new QwtSymbol( QwtSymbol::Ellipse,
        QBrush( Qt::yellow ), QPen( Qt::red, 2 ), QSize( 8, 8 ) );
    d_curve_ftl_red->setSymbol(symbol2);
    d_curve_ftl_red->setSamples( points_r );
    d_curve_ftl_red->attach( plot );


    d_curve_ftl_green = new QwtPlotCurve();
    d_curve_ftl_green->setTitle( tr("绿光亮度 ") );
    d_curve_ftl_green->setPen( Qt::green, 2 );
    d_curve_ftl_green->setRenderHint( QwtPlotItem::RenderAntialiased, true );

    d_curve_ftl_green->attach( plot );
    QwtSymbol *symbol3 = new QwtSymbol( QwtSymbol::Ellipse,
        QBrush( Qt::yellow ), QPen( Qt::red, 2 ), QSize( 8, 8 ) );
    d_curve_ftl_green->setSymbol(symbol3);
    d_curve_ftl_green->setSamples( points_g);
    d_curve_ftl_green->attach( plot );


    d_curve_ftl_blue = new QwtPlotCurve();
    d_curve_ftl_blue->setTitle( tr("蓝光亮度 ") );
    d_curve_ftl_blue->setPen( Qt::blue, 2 );
    d_curve_ftl_blue->setRenderHint( QwtPlotItem::RenderAntialiased, true );

    d_curve_ftl_blue->attach( plot );
    QwtSymbol *symbol4 = new QwtSymbol( QwtSymbol::Ellipse,
        QBrush( Qt::yellow ), QPen( Qt::red, 2 ), QSize( 8, 8 ) );
    d_curve_ftl_blue->setSymbol(symbol4);
    d_curve_ftl_blue->setSamples( points_b);
    d_curve_ftl_blue->attach( plot );
    plot->replot();
}

void Dialog::DrawSplQwtPlot( QString cinema_hall)
{
//    if(splview != NULL)
//    {
//        delete splview;
//    }
    splview = new SplView(ui->SplViewWidget);

    QList<double> list_lc, list_rc, list_cc, list_hb, list_la, list_ra;
    QString sql = QObject::tr("select lc, rc, cc, hb, la, ra from tb_dev_test_results where cinema_hall = '%1'").arg(cinema_hall);
    model_detail->setQuery(sql, db);
    QModelIndex index;


    for(int i = 0; i<5; i++)
    {

        list_lc.append(model_detail->data(model_detail->index(i, 0)).toDouble());
        list_rc.append(model_detail->data(model_detail->index(i, 1)).toDouble());
        list_cc.append(model_detail->data(model_detail->index(i, 2)).toDouble());
        list_hb.append(model_detail->data(model_detail->index(i, 3)).toDouble());
        list_la.append(model_detail->data(model_detail->index(i, 4)).toDouble());
        list_ra.append(model_detail->data(model_detail->index(i, 5)).toDouble());

    }
    cout<<"ccc: "<<list_lc.at(0)<<endl;
    splview->setFixedSize(ui->SplViewWidget->width(), ui->SplViewWidget->height());
    splview->setAudioCounts(6);
    splview->setAudioValue(list_lc.at(0), 0, Qt::green); //Left
    splview->setAudioValue(list_rc.at(0), 1, Qt::green); //Right
    splview->setAudioValue(list_cc.at(0), 2, Qt::red);   //Center
    splview->setAudioValue(list_hb.at(0), 3, Qt::green);  //LFE
    splview->setAudioValue(list_la.at(0), 4, Qt::green); //LS
    splview->setAudioValue(list_ra.at(0), 5, Qt::green); //RS
    splview->reDraw();
}

void Dialog:: DrawColorAreaXy( QString cinema_hall)
{
    QString sql = QObject::tr("select rx, ry, gx, gy, bluex, bluey from tb_dev_test_results where cinema_hall = '%1' order by id limit 5").arg(cinema_hall);
    cout<<sql.toStdString()<<endl;
    model_detail->setQuery(sql, db);
    QList<QSizeF> list_r, list_g, list_b;
    list_r.clear(); list_g.clear(); list_b.clear();
    double rx = 0.0, ry = 0.0, gx = 0.0, gy = 0.0, bx = 0.0, by = 0.0;
    for(int i = 0; i<5; i++)
    {
         rx = model_detail->data(model_detail->index(i, 0)).toDouble();
         ry = model_detail->data(model_detail->index(i, 1)).toDouble();

         gx = model_detail->data(model_detail->index(i, 2)).toDouble();
         gy = model_detail->data(model_detail->index(i, 3)).toDouble();

         bx = model_detail->data(model_detail->index(i, 4)).toDouble();
         by = model_detail->data(model_detail->index(i, 5)).toDouble();

        list_r.append(QSizeF(rx, ry));
        list_g.append(QSizeF(gx, gy));
        list_b.append(QSizeF(bx, by));

    }



    if(colorview == NULL)
        colorview = new ColorView(ui->colorViewWidget);
    colorview->setFixedSize(ui->colorViewWidget->width(), ui->colorViewWidget->height());

    for(int i = 0; i<list_r.size(); i++)
    {
        colorview->addColor(list_r.at(i), list_g.at(i), list_b.at(i));
    }
    colorview->reDraw();

}

void Dialog:: DrawCurve(QwtPlot *plot, QString title,     QPolygonF points)
{

     plot->setTitle( title );
     plot->setCanvasBackground( QColor(120,40,44) );
     plot->setAxisScale( QwtPlot::yLeft, 0.0, 10.0 );
     legend = new QwtLegend();
     plot->insertLegend( legend );

    grid = new QwtPlotGrid();

    grid->attach( plot );

    curve_demo = new QwtPlotCurve();
    curve_demo->setTitle( title );
    curve_demo->setPen( Qt::blue, 4 ),
    curve_demo->setRenderHint( QwtPlotItem::RenderAntialiased, true );

    QwtSymbol *symbol = new QwtSymbol( QwtSymbol::Ellipse,
        QBrush( Qt::yellow ), QPen( Qt::red, 2 ), QSize( 8, 8 ) );
    curve_demo->setSymbol( symbol );

    curve_demo->setSamples( points );
    curve_demo->attach(plot);


}

void Dialog::on_pushButton_13_clicked()
{
    QPrinter printer;
    QSizeF sizef(200, 100);
    QwtPlotRenderer render;
    QString spl_path = "d:\\spl.jpg";
    QString ftl_path = "d:\\ftl.jpg";
    QString cct_path = "d:\\cct.jpg";
    QString temperature_path = "d:\\temperature.jpg";
    QString xy_path = "d:\\xy.jpg";
     QPainter painter(&printer);
     QPixmap image;
     image = image.grabWidget(ui->SplViewWidget,0,0,ui->SplViewWidget->width(), ui->SplViewWidget->height());
     QRect rect = painter.viewport();
     QSize size = image.size();
     size.scale(rect.size(), Qt::KeepAspectRatio);
     //此处保证图片显示完整
     painter.setViewport(rect.x(), rect.y(),size.width(), size.height());
     painter.setWindow(image.rect());
     painter.drawPixmap(0,0,image);
     image.save(spl_path);

     image = image.grabWidget(ui->colorViewWidget,0,0,ui->colorViewWidget->width(), ui->colorViewWidget->height());
      rect = painter.viewport();
      size = image.size();
     size.scale(rect.size(), Qt::KeepAspectRatio);
     //此处保证图片显示完整
     painter.setViewport(rect.x(), rect.y(),size.width(), size.height());
     painter.setWindow(image.rect());
     painter.drawPixmap(0,0,image);
     image.save(xy_path);
     render.exportToc(ui->FtlqwtPlot, ftl_path, sizef, 85);
     render.exportToc(ui->ColorTempqwtPlot, cct_path, sizef, 85);
     render.exportToc(ui->TempqwtPlot, temperature_path, sizef, 85);




    HtmlTable table;

    table.BeginTr();
    table.AddTd("<img src = " + spl_path +"/>");
    table.EndTr();

    table.BeginTr();
    table.AddTd("<img src = " + ftl_path +"/>");
    table.EndTr();

    table.BeginTr();
    table.AddTd("<img src = " + cct_path +"/>");
    table.EndTr();

    table.BeginTr();
    table.AddTd("<img src = " + xy_path +"/>");
    table.EndTr();


    table.BeginTr();
    table.AddTd("<img src = " + temperature_path +"/>");
    table.EndTr();

    QString html = "<html><body>" + table.ToHtmlTableString() + "</body></html>";
    QFile file("D:\\html.txt");
    file.open(QIODevice::WriteOnly);
    QTextStream out(&file);
    out<<html<<endl;
    file.close();
    reportWindow = new ReportDocumentMainWindow(this);
    reportWindow->show();
}




void Dialog::on_qry_select_cinema_hall_comboBox_currentTextChanged(const QString &arg1)
{
    QString sql ;
    if(arg1 == tr("所有影厅"))
    {
        sql = QObject::tr("select * from tb_dev_test_results");
    }
    else
    {
       sql = QObject::tr("select * from tb_dev_test_results where cinema_hall like '%1'").arg(arg1);
    }

    model->setQuery(sql, db);
    ui->TotaltableView->setModel(model);
}

void Dialog::on_pushButton_7_clicked()
{
    QString sql;
    QString start_time = ui->qry_start_dateTimeEdit->text();
    QString end_time = ui->qry_end_dateTimeEdit_2->text();
    QString cinema_hall = ui->qry_select_cinema_hall_comboBox->currentText();

    QDateTime stime1 = QDateTime::fromString(start_time, "yyyy-MM-dd hh:mm:ss");
    QDateTime etime1 =  QDateTime::fromString(end_time, "yyyy-MM-dd hh:mm:ss");


    if(cinema_hall == tr("所有影厅"))
    {
       sql = QObject::tr("select * from tb_dev_test_results where 1=1");
    }
    else
    {
         sql = QObject::tr("select * from tb_dev_test_results where cinema_hall like '%1'").arg(cinema_hall);
    }

    // 时间的输入校验

    if(stime1 > etime1)
    {
        QMessageBox::information(this, tr("错误"), tr("开始时间应该小于结束时间"));
        return;
    }
    QString stime = ui->qry_start_dateTimeEdit->text();
    QString etime = ui->qry_end_dateTimeEdit_2->text();


    sql += QObject::tr(" and stime between '%1' and '%2'").arg(stime).arg(etime);
    cout<<sql.toStdString()<<endl;
    model->setQuery(sql, db);

}



void Dialog::on_select_cinema_comboBox_currentTextChanged(const QString &arg1)
{
    QString sql;
    if(arg1 != tr("选择影厅"))
    {
        sql = QObject::tr("select * from tb_dev_test_results where cinema_hall like '%1' limit 5 ").arg(arg1);
    }
    else
    {
        sql = QObject::tr("select * from tb_dev_test_results");
    }
    model_detail->setQuery(sql, db);
    model_detail_view->setQuery(sql, db);
    model_std->setQuery(QObject::tr("select * from std_test where cinema_hall = '%1'").arg(arg1), db);

    ui->DetailtableView->setModel(model_detail_view);
    if(model_detail->lastError().isValid())
    {
        cout<<model_detail->lastError().text().toStdString()<<endl;
        QMessageBox::information(this, "Error", model_detail->lastError().text());
        return;
    }
    DrawColorQwtPlot(ui->ColorTempqwtPlot, arg1);
    DrawTempQwtPlot(ui->TempqwtPlot, arg1);
    DrawSplQwtPlot(arg1);
    DrawSplQwtPlot(arg1);
    DrawColorAreaXy( arg1);
}

void Dialog::clearForSetForm()
{
    ui->lclineEdit->setText("");
    ui->rclineEdit->setText("");
    ui->cclineEdit->setText("");
    ui->hblineEdit->setText("");
    ui->lrlineEdit->setText("");
    ui->rrlineEdit->setText("");
    ui->iplineEdit->setText("");
    ui->whlineEdit->setText("");
    ui->hallcomboBox->clear();
}



void Dialog::mod_cinema_hall()
{
    QString sql;
    QString cinema_hall = ui->hallcomboBox->currentText().trimmed();
    QString dev_ip = ui->iplineEdit->text();
    double lc = ui->lclineEdit->text().trimmed().toFloat();
    double rc = ui->rclineEdit->text().trimmed().toFloat();
    double cc = ui->cclineEdit->text().trimmed().toFloat();
    double hb = ui->hblineEdit->text().trimmed().toFloat();
    double lr = ui->lrlineEdit->text().trimmed().toFloat();
    double rr = ui->rrlineEdit->text().trimmed().toFloat();
    double wh = ui->whlineEdit->text().trimmed().toFloat();
    QString stime = ui->start_timeEdit_3->text();
    QString etime = ui->end_timeEdit->text();


    sql = QObject::tr("update tb_dev_corrected_value_conf set "
                      "dev_ip = '%1', "
                      "fix_ftl = '%2',"
                      "fix_lc = '%3', "
                      "fix_rc = '%4', "
                      "fix_cc = '%5', "
                      "fix_hb = '%6', "
                      "fix_la = '%7', "
                      "fix_ra = '%8', "
                      "start_datetime = '%9', "
                      "end_datetime = '%10' "
                      "where cinema_hall = '%11' "
                      )
            .arg(dev_ip)
            .arg(wh)
            .arg(lc)
            .arg(rc)
            .arg(cc)
            .arg(hb)
            .arg(lr)
            .arg(rr)
            .arg(stime)
            .arg(etime)
            .arg(cinema_hall);


    model_conf->setQuery(sql, db);
    if(model_conf->lastError().isValid())
    {
        cout<<sql.toStdString()<<endl;
        cout<<model_conf->lastError().text().toStdString()<<endl;
        QMessageBox::information(this, "error", model_conf->lastError().text());

    }
    cout<<sql.toStdString()<<endl;
    QMessageBox::information(this, "inormation", tr("影厅信息已被修改"));

}

void Dialog::add_cinema_hall()
{
    QString sql;
    QString cinema_name = ui->cinema_lineEdit->text().trimmed();
    QString cinema_hall = ui->hallcomboBox->currentText().trimmed();
    QString dev_ip = ui->iplineEdit->text();
    double lc = ui->lclineEdit->text().trimmed().toFloat();
    double rc = ui->rclineEdit->text().trimmed().toFloat();
    double cc = ui->cclineEdit->text().trimmed().toFloat();
    double hb = ui->hblineEdit->text().trimmed().toFloat();
    double lr = ui->lrlineEdit->text().trimmed().toFloat();
    double rr = ui->rrlineEdit->text().trimmed().toFloat();
    double wh = ui->whlineEdit->text().trimmed().toFloat();
    QString stime = ui->start_timeEdit_3->text();
    QString etime = ui->end_timeEdit->text();


    sql = QObject::tr("insert into tb_dev_corrected_value_conf("
                      "cinema_name, cinema_hall, dev_ip,"
                      "fix_lc, fix_rc, fix_cc, fix_hb, "
                      "fix_la, fix_ra, start_datetime, end_datetime,fix_ftl "
                      ")values("
                      "'%1', '%2', '%3',"
                      "'%4', '%5', '%6', '%7',"
                      "'%8', '%9', '%10', '%11','%12'"
                      ");")
                    .arg(cinema_name).arg(cinema_hall).arg(dev_ip)
                    .arg(lc).arg(rc).arg(cc).arg(hb)
                    .arg(lr).arg(rr).arg(stime).arg(etime).arg(wh)

            ;
    cout<<sql.toStdString()<<endl;

    model_conf->setQuery(sql, db);
    if (model_conf->lastError().isValid())
    {
        cout<< model_conf->lastError().text().toStdString()<<endl;
        qDebug() << model_conf->lastError();
        QMessageBox::information(this, "error", model_conf->lastError().text() );
        return;
    }
    QMessageBox::information(this, "information", tr("影厅添加成功"));
}

// 设置修正参数 点击"确定"按钮
void Dialog::on_pushButton_2_clicked()
{
    switch(oper)
    {
        case ADD_HALL:
         add_cinema_hall();
             break;
        case MOD_HALL:
            mod_cinema_hall();
            break;
        default:
            mod_cinema_hall();

    }
}


// 添加影厅
void Dialog::on_radioButton_2_clicked()
{
    oper = ADD_HALL;
    cout<<oper<<endl;
   // clearForSetForm();
}

// 修改影厅
void Dialog::on_radioButton_clicked()
{
    oper = MOD_HALL;
    cout<<oper<<endl;
}

void Dialog::on_select_cinema_comboBox_3_currentTextChanged(const QString &arg1)
{
    QString sql = QObject::tr("select * from tb_dev_corrected_value_conf where cinema_hall='%1'").arg(arg1);
    model_conf->setQuery(sql, db);
    QString cinema_hall = model_conf->data(model_conf->index(0, 2)).toString();
    QString dev_ip = model_conf->data(model_conf->index(0, 3)).toString();
    QString wh = model_conf->data(model_conf->index(0, 4)).toString();
    QString lc = model_conf->data(model_conf->index(0, 5)).toString();
    QString rc = model_conf->data(model_conf->index(0, 6)).toString();
    QString cc = model_conf->data(model_conf->index(0, 7)).toString();
    QString hb = model_conf->data(model_conf->index(0, 8)).toString();
    QString la = model_conf->data(model_conf->index(0, 9)).toString();
    QString ra = model_conf->data(model_conf->index(0, 10)).toString();
    QTime stime = model_conf->data(model_conf->index(0, 11)).toTime();
    QTime etime = model_conf->data(model_conf->index(0, 12)).toTime();


    ui->iplineEdit->setText(dev_ip);
    ui->whlineEdit->setText(wh);
    ui->lclineEdit->setText(lc);
    ui->rclineEdit->setText(rc);
    ui->cclineEdit->setText(cc);
    ui->hblineEdit->setText(hb);
    ui->lrlineEdit->setText(la);
    ui->rrlineEdit->setText(ra);
    ui->start_timeEdit_3->setTime(stime);
    ui->end_timeEdit->setTime(etime);
    ui->hallcomboBox->setCurrentText(arg1);

}

void Dialog::on_standard_select_cinema_hall_comboBox_2_currentTextChanged(const QString &arg1)
{

    QString sql;

    sql = tr("select * from std_test where cinema_hall = '%1'").arg(arg1);
    cout<<sql.toStdString()<<endl;


    model_std = new QSqlQueryModel(this);
    model_std->setQuery(sql, db);

    if(model_std->lastError().isValid())
    {
        QMessageBox::information(this, "error", model_std->lastError().text());
        return ;
    }
    QString lc = model_std->data(model_std->index(0, 1)).toString();
    QString rc = model_std->data(model_std->index(0, 2)).toString();
    QString cc = model_std->data(model_std->index(0, 3)).toString();
    QString hb = model_std->data(model_std->index(0, 4)).toString();
    QString la = model_std->data(model_std->index(0, 5)).toString();
    QString ra = model_std->data(model_std->index(0, 6)).toString();
    QString white_ftl = model_std->data(model_std->index(0, 7)).toString();
    QString red_ftl = model_std->data(model_std->index(0, 8)).toString();
    QString green_ftl = model_std->data(model_std->index(0, 9)).toString();
    QString blue_ftl = model_std->data(model_std->index(0, 10)).toString();
    QString temp = model_std->data(model_std->index(0, 11)).toString();
    QString cct = model_std->data(model_std->index(0, 12)).toString();

    ui->lclineEdit_2->setText(lc);
    ui->rclineEdit_2->setText(rc);
    ui->cclineEdit_2->setText(cc);
    ui->hblineEdit_2->setText(hb);
    ui->lrlineEdit_2->setText(la);
    ui->rrlineEdit_2->setText(ra);
    ui->whlineEdit_2->setText(white_ftl);
    ui->red_ftl_lineEdit->setText(red_ftl);
    ui->green_ftl_lineEdit->setText(green_ftl);
    ui->blue_ftl_lineEdit->setText(blue_ftl);
    ui->temp_lineEdit_2->setText(temp);
    ui->cctlineEdit_2->setText(cct);

    ui->lc_Slider->setValue(lc.toFloat());
    ui->rc_Slider->setValue(rc.toFloat());
    ui->cc_Slider->setValue(cc.toFloat());
    ui->hb_Slider->setValue(hb.toFloat());
    ui->la_Slider->setValue(la.toFloat());
    ui->ra_Slider->setValue(ra.toFloat());
    ui->red_ftl_Slider->setValue(red_ftl.toFloat());
    ui->blue_ftl_Slider->setValue(blue_ftl.toFloat());
    ui->green_ftl_Slider->setValue(green_ftl.toFloat());
    ui->cct_Slider->setValue(cct.toDouble());
    ui->temp_Slider->setValue(temp.toDouble());
}

// 标准选项卡中的"确定"按钮

void Dialog::on_pushButton_5_clicked()
{
   QString lc = ui->lclineEdit_2->text().trimmed();
   QString rc = ui->rclineEdit_2->text().trimmed();
   QString cc = ui->cclineEdit_2->text().trimmed();
   QString hb = ui->hblineEdit_2->text().trimmed();

   QString lr = ui->lrlineEdit_2->text().trimmed();
   QString rr = ui->rrlineEdit_2->text().trimmed();
   QString wh = ui->whlineEdit_2->text().trimmed();
   QString red_ftl = ui->red_ftl_lineEdit->text().trimmed();

   QString green_ftl = ui->green_ftl_lineEdit->text().trimmed();
   QString blue_ftl = ui->blue_ftl_lineEdit->text().trimmed();
   QString temp = ui->temp_lineEdit_2->text().trimmed();
   QString cct = ui->cctlineEdit_2->text().trimmed();

   QString cinema_hall = ui->standard_select_cinema_hall_comboBox_2->currentText();
   model_std = new QSqlQueryModel(this);
   QString sql = tr("update std_test set "
                    "lc = '%1', "
                    "rc = '%2', "
                    "cc = '%3', "
                    "hb = '%4', "
                    "la = '%5', "
                    "ra = '%6', "
                    "white_ftl = '%7', "
                    "red_ftl   = '%8', "
                    "green_ftl = '%9', "
                    "blue_ftl  = '%10', "
                    "temp      = '%11', "
                    "cct       = '%12' "
                    "where cinema_hall like '%13'; "
                    )
           .arg(lc)
           .arg(rc)
           .arg(cc)
           .arg(hb)
           .arg(lr)
           .arg(rr)
           .arg(wh)
           .arg(red_ftl)
           .arg(green_ftl)
           .arg(blue_ftl)
           .arg(temp)
           .arg(cct)
           .arg(cinema_hall);


   model_std->setQuery(sql, db);
   cout<<sql.toStdString()<<endl;
   if(model_std->lastError().isValid())
   {
        cout<<model_std->lastError().text().toStdString()<<endl;
        QMessageBox::information(this, "error", model_std->lastError().text());
        return;
   }

   QMessageBox::information(this, "inormation", tr("成功修改"));

   delete model_std;
}

void Dialog::on_pushButton_8_clicked()
{
    reloadCombo();
}

void Dialog::on_pushButton_3_clicked()
{
    QString user = ui->user_lineEdit->text().trimmed();
    QString passwd = ui->passwd_lineEdit->text().trimmed();
    QString email = ui->email_lineEdit->text().trimmed();
    QString tel = ui->tel_lineEdit->text().trimmed();
    QString cinema = ui->cinema_lineEdit->text().trimmed();

    QString sql = QObject::tr("update user set "
                              " username = '%1',"
                              " password = '%2',"
                              " email = '%3', "
                              " tel = '%4' "
                              " where id = 1"
                              ).arg(user).arg(passwd).arg(email).arg(tel).arg(cinema);

    model_user->setQuery(sql, db);
    if(model_user->lastError().isValid())
    {
        QMessageBox::information(this, "error", model_user->lastError().text());
        return ;
    }
    else
    {
        QMessageBox::information(this, "information", QObject::tr("成功设置用户信息"));
    }


    sql = QObject::tr("select * from user");
    model_user->setQuery(sql, db);


}

void Dialog::on_pushButton_4_clicked()
{
    close();
}

void Dialog::on_pushButton_clicked()
{
    close();
}

void Dialog::on_pushButton_6_clicked()
{
    close();
}

void Dialog::on_auto_comboBox_currentTextChanged(const QString &arg1)
{
    ui->auto_groupBox->setTitle(arg1);
}

void Dialog::on_pushButton_10_clicked()
{

    modifyPasswordDialog.setModal(true);
    modifyPasswordDialog.exec();

   QString username = modifyPasswordDialog.username;
   QString new_password = modifyPasswordDialog.new_password;

   if(username == "")
   {
       return ;
   }
    QString sql = tr("update user set password='%1' where username = '%2' ").arg(new_password).arg(username);
    cout<<"sql="<<sql.toStdString()<<endl;
    model_user->setQuery(sql, db);
    if(model_user->lastError().isValid())
    {
        QMessageBox::information(this, tr("Error"), tr("Failed to exec sql."));
        return ;
    }

    QMessageBox::information(this, tr("success"), tr("success to update password"));

  model_user->setQuery("select * from user", db);

}

void Dialog::on_AddUserpushButton_clicked()
{
    addUserDialog.setModal(true);
    addUserDialog.exec();

    QString username = addUserDialog.username;
    QString password = addUserDialog.password;
    QString email = addUserDialog.Email;
    QString tel = addUserDialog.tel;

    if(username == "")
    {
        return ;
    }
    QString sql = tr("select * from user where username = '%1' ").arg(username);
    model_user->setQuery(sql, db);
    if(model_user->rowCount()>= 1)
    {
        QMessageBox::information(this, tr("Error"), tr("The user name has already been used"));
        return ;
    }

     sql = tr("insert into user(username, password, email, tel)values('%1', '%2', '%3', '%4')")
            .arg(username)
            .arg(password)
            .arg(email)
            .arg(tel);

    model_user->setQuery(sql, db);
    if(model_user->lastError().isValid())
    {
        qDebug()<<sql<<endl;
        QMessageBox::information(this, tr("Error"), tr("Failed to execute sql"));
        return;
    }

    model_user->setQuery("select * from user", db);


}

void Dialog::on_ModifyUserpushButton_12_clicked()
{
    modifyUserDialog.setModal(true);

    int row = currRow;
   QString username = model_user->data(model_user->index(row, 1)).toString();
   QString password = model_user->data(model_user->index(row, 2)).toString();
   QString Email = model_user->data(model_user->index(row, 3)).toString();
   QString tel = model_user->data(model_user->index(row, 4)).toString();

   modifyUserDialog.username = username;
   modifyUserDialog.password = password;
   modifyUserDialog.email = Email;
   modifyUserDialog.tel = tel;
   modifyUserDialog.setUi();
   modifyUserDialog.exec();


   username = modifyUserDialog.username1;
   password = modifyUserDialog.password1;
   Email = modifyUserDialog.email1;
   tel = modifyUserDialog.tel1;

   QString sql = tr("update user set "
                    " password = '%1', "
                    " email = '%2', "
                    " tel = '%3' "
                    " where username = '%4'"
                    ).arg(password).arg(Email).arg(tel).arg(username);

   model_user->setQuery(sql, db);
   if(model_user->lastError().isValid())
   {
       QMessageBox::information(this, tr("Error"), tr("Failed to execute sql"));
       return;
   }

   model_user->setQuery("select * from user", db);


}

void Dialog::on_loginOutpushButton_clicked()
{
    LoginDialog *loginDialog = new LoginDialog(this);
    loginDialog->setModal(true);
    loginDialog->exec();

    QString username = loginDialog->username;
    QString password = loginDialog->password;

    if(username == "")
        return ;
    QString sql = tr("select * from user where username = '%1' and password = '%2'").arg(username).arg(password);
    model_user->setQuery(sql, db);
    if(model_user->lastError().isValid())
    {
        QMessageBox::information(this, tr("Error"), tr("Failed to execute sql"));
        return;
    }
    if(model_user->rowCount()!= 1)
    {
        QMessageBox::information(this,tr("Error"), tr("Invalid user name and password"));
        return ;
    }

    ui->label_18->setText(username);
    model_user->setQuery("select * from user", db);
}

void Dialog::loadTree()
{
     QList<QTreeWidgetItem *> rootList;
    ui->treeWidget->setColumnCount(1);

    model_conf->setQuery("select * from tb_dev_corrected_value_conf", db);
    int cnt = model_conf->rowCount();
    QStringList hall_list;
    for(int i=0; i<cnt; i++)
    {
        hall_list<<model_conf->data(model_conf->index(i, 2)).toString();
        QTreeWidgetItem  *root  =  new QTreeWidgetItem(ui->treeWidget, QStringList(hall_list.at(i)));

        QTreeWidgetItem  *left_spl_leaf  =  new QTreeWidgetItem(root,  QStringList(QString(tr("左声道"))));
        root->addChild(left_spl_leaf);
        left_spl_leaf->setCheckState(0, Qt::Checked);

        QTreeWidgetItem  *right_spl_leaf  =  new QTreeWidgetItem(root, QStringList(QString(tr("右声道"))));
        right_spl_leaf->setCheckState(0, Qt::Checked);
        root->addChild(right_spl_leaf);


        QTreeWidgetItem  *cc_spl_leaf  =  new QTreeWidgetItem(root, QStringList(QString(tr("中置"))));
        cc_spl_leaf->setCheckState(0, Qt::Checked);
        root->addChild(cc_spl_leaf);

        QTreeWidgetItem  *hb_spl_leaf  =  new QTreeWidgetItem(root, QStringList(QString(tr("重低音"))));
        hb_spl_leaf->setCheckState(0, Qt::Checked);
        root->addChild(hb_spl_leaf);

        QTreeWidgetItem  *left_around_spl_leaf  =  new QTreeWidgetItem(root, QStringList(QString(tr("左环"))));
        left_around_spl_leaf->setCheckState(0, Qt::Checked);
        root->addChild(left_around_spl_leaf);

        QTreeWidgetItem  *right_around_spl_leaf  =  new QTreeWidgetItem(root, QStringList(QString(tr("右环"))));
        right_around_spl_leaf->setCheckState(0, Qt::Checked);
        root->addChild(right_around_spl_leaf);

        QTreeWidgetItem  *left_behind_spl_leaf  =  new QTreeWidgetItem(root, QStringList(QString(tr("左后"))));
        left_behind_spl_leaf->setCheckState(0, Qt::Checked);
        root->addChild(left_behind_spl_leaf);

        QTreeWidgetItem  *right_behind_spl_leaf  =  new QTreeWidgetItem(root, QStringList(QString(tr("右后"))));
        right_behind_spl_leaf->setCheckState(0, Qt::Checked);
        root->addChild(right_behind_spl_leaf);


        QTreeWidgetItem  *white_ftl_leaf  =  new QTreeWidgetItem(root, QStringList(QString(tr("白光亮度"))));
        white_ftl_leaf->setCheckState(0, Qt::Checked);
        root->addChild(white_ftl_leaf);

        QTreeWidgetItem  *red_ftl_leaf  =  new QTreeWidgetItem(root, QStringList(QString(tr("红光色度"))));
        red_ftl_leaf->setCheckState(0, Qt::Checked);
        root->addChild(red_ftl_leaf);

        QTreeWidgetItem  *green_ftl_leaf  =  new QTreeWidgetItem(root, QStringList(QString(tr("绿光色度"))));
        green_ftl_leaf->setCheckState(0, Qt::Checked);
        root->addChild(green_ftl_leaf);

        QTreeWidgetItem  *blue_ftl_leaf  =  new QTreeWidgetItem(root, QStringList(QString(tr("蓝光色度"))));
        blue_ftl_leaf->setCheckState(0, Qt::Checked);
        root->addChild(blue_ftl_leaf);

        QTreeWidgetItem  *temperater_leaf =  new QTreeWidgetItem(root, QStringList(QString(tr("温度(摄氏)"))));
        temperater_leaf->setCheckState(0, Qt::Checked);
        root->addChild(temperater_leaf);


        QTreeWidgetItem  *color_temperator_leaf  =  new QTreeWidgetItem(root, QStringList(QString(tr(" 色温"))));
        color_temperator_leaf->setCheckState(0, Qt::Checked);
        root->addChild(color_temperator_leaf);


        rootList << root;

    }

    ui->treeWidget->insertTopLevelItems(0, rootList);

 }

void Dialog::on_preview_pushButton_clicked()
{

    QWebView *view = new QWebView();
    view->load(QUrl("http://localhost:8000/report"));
    view->show();
}

void Dialog::on_exit_pushButton_9_clicked()
{
    close();
}

void Dialog::on_export_pushButton_clicked()
{

}

void Dialog::installTranslator()
{
      translator.load("english.qm", qApp->applicationDirPath());
      qApp->installTranslator(&translator);
}

void Dialog::on_language_comboBox_currentIndexChanged(int index)
{
    QSettings *settings = new QSettings("config.ini", QSettings::IniFormat ) ;
    if(index == 0)
    {
        settings->setValue("language", "en");
    }
    else
    {
        settings->setValue("language", "cn");
    }

    delete settings;
 //   QMessageBox::information(this, tr("提示"), tr("请重新启动软件,使之生效！"));
}


 void Dialog::changeEvent ( QEvent * event )
 {
     if (event->type() == QEvent::LanguageChange)
     {
//        ui->AutoSetabWidget->setTabText(0, QObject::tr("Totals"));

     }
     else
    {
        QWidget::changeEvent(event);
    }
 }

void Dialog::on_pushButton_11_clicked()
{
    QString username = ui->lineEdit->text().trimmed();

    QString sql = tr("select * from user");
    if(username == "")
    {
        model_user->setQuery(sql, db);
        qDebug()<<sql<<endl;
        if(model_user->lastError().isValid())
        {
            QMessageBox::information(this, tr("Error"), tr("An error occurs connecting to the database, Failed to perform the SQL statement."));
            return ;
        }
    }

    sql = tr("select * from user where username like '%%1%'").arg(username);

    model_user->setQuery(sql, db);
    if(model_user->lastError().isValid())
    {
         qDebug()<<sql<<endl;
        QMessageBox::information(this, tr("Error"), tr("An error occurs connecting to the database, Failed to perform the SQL statement."));
        return ;
    }


}

void Dialog::on_usertableView_clicked(const QModelIndex &index)
{
    currRow = index.row();

}

void Dialog::on_RemoveUserpushButton_14_clicked()
{
    int row = currRow;
    QString username = model_user->data(model_user->index(row, 1)).toString();

  QMessageBox::StandardButton rb = QMessageBox::warning(NULL,
                                                        tr("Warning"),
                                                        tr("Do you sure want to delete?"),
                                                        QMessageBox::Yes | QMessageBox::No );

  if(rb == QMessageBox::Yes)
  {
          QString sql = tr("delete from user where username = '%1'").arg(username);
          model_user->setQuery(sql, db);
          if(model_user->lastError().isValid())
          {
               QMessageBox::information(this, tr("Error"), tr("An error occurs connecting to the database, Failed to perform the SQL statement."));
               return ;
          }

          sql = tr("select * from user");
          model_user->setQuery(sql, db);
  }
  else
  {
      return ;
  }
}
void Dialog::alert(QString msg)
{
    QMessageBox::information(this, "", msg);
}

void Dialog::on_select_cinema_comboBox_4_currentTextChanged(const QString &arg1)
{
     QString sql = tr("select * from offset_test where o_cinema_hall = '%1'").arg(arg1);
     model_offset->setQuery(sql, db);
     if(model_offset->lastError().isValid())
     {
         alert(model_offset->lastError().text());
         return;
     }


     QString o_lc = model_offset->data(model_offset->index(0, 1)).toString();
     QString o_rc = model_offset->data(model_offset->index(0, 2)).toString();
     QString o_cc = model_offset->data(model_offset->index(0, 3)).toString();
     QString o_hb = model_offset->data(model_offset->index(0, 4)).toString();
     QString o_la = model_offset->data(model_offset->index(0, 5)).toString();
     QString o_ra = model_offset->data(model_offset->index(0, 6)).toString();
     QString o_white_ftl = model_offset->data(model_offset->index(0, 7)).toString();
     QString o_red_ftl = model_offset->data(model_offset->index(0, 8)).toString();
     QString o_green_ftl = model_offset->data(model_offset->index(0, 9)).toString();
     QString o_blue_ftl = model_offset->data(model_offset->index(0, 10)).toString();
     QString o_cct = model_offset->data(model_offset->index(0, 11)).toString();
     QString o_temp = model_offset->data(model_offset->index(0, 12)).toString();

    ui->o_lclineEdit_8->setText(o_lc);
    ui->o_rclineEdit_9->setText(o_rc);
    ui->o_cc_lineEdit_10->setText(o_cc);
    ui->o_hb_lineEdit_21->setText(o_hb);
    ui->o_lalineEdit_22->setText(o_la);
    ui->o_ralineEdit_23->setText(o_ra);
    ui->o_white_ftl_lineEdit_2->setText(o_white_ftl);
    ui->o_red_ftllineEdit_3->setText(o_red_ftl);
    ui->o_green_ftl_lineEdit_5->setText(o_green_ftl);
    ui->o_blue_ftl_lineEdit_4->setText(o_blue_ftl);
    ui->o_cct_lineEdit_6->setText(o_cct);
    ui->o_temp_lineEdit_7->setText(o_temp);

}

void Dialog::on_pushButton_9_clicked()
{
    QString o_cinema_hall = ui->select_cinema_comboBox_4->currentText();
    QString o_lc =  ui->o_lclineEdit_8->text().trimmed();
    QString o_rc =  ui->o_rclineEdit_9->text().trimmed();
    QString o_cc =  ui->o_cc_lineEdit_10->text().trimmed();
    QString o_hb =  ui->o_hb_lineEdit_21->text().trimmed();
    QString o_la =  ui->o_lalineEdit_22->text().trimmed();
    QString o_ra =  ui->o_ralineEdit_23->text().trimmed();
    QString o_white_ftl =  ui->o_white_ftl_lineEdit_2->text().trimmed();
    QString o_red_ftl =  ui->o_red_ftllineEdit_3->text().trimmed();
    QString o_green_ftl =  ui->o_green_ftl_lineEdit_5->text().trimmed();
    QString o_blue_ftl =  ui->o_blue_ftl_lineEdit_4->text().trimmed();
    QString o_cct =  ui->o_cct_lineEdit_6->text().trimmed();
    QString o_temp =  ui->o_temp_lineEdit_7->text().trimmed();

    QString sql_valid = tr("select cinema_hall from tb_dev_corrected_value_conf where cinema_hall = '%1'").arg(o_cinema_hall);
    model_offset->setQuery(sql_valid, db);
    if(model_offset->rowCount() <= 0)
    {
        QString sql = tr("insert into offset(o_cinema_hall)value('%1')").arg(o_cinema_hall);
        model_offset->setQuery(sql, db);
        if(model_offset->lastError().isValid())
        {
            alert(model_offset->lastError().text());
            return ;
        }
//        model_offset->setQuery("select * from offset_test", db);
    }

    QString sql2 = tr("update offset_test set "
                      " o_lc = '%1', "
                       " o_rc = '%2', "
                       " o_cc = '%3', "
                       " o_hb = '%4', "
                       " o_la = '%5', "
                       " o_ra = '%6', "
                       " o_white_ftl = '%7', "
                       " o_red_ftl = '%8', "
                       " o_green_ftl = '%9', "
                       " o_blue_ftl = '%10', "
                      " o_cct = '%11', "
                      " o_temp = '%12' "
                      " where o_cinema_hall = '%13' "
                      )
            .arg(o_lc).arg(o_rc).arg(o_cc).arg(o_hb).arg(o_la).arg(o_ra)
            .arg(o_white_ftl).arg(o_red_ftl).arg(o_green_ftl).arg(o_blue_ftl)
            .arg(o_cct).arg(o_temp).arg(o_cinema_hall);

        model_offset->setQuery(sql2, db);
       qDebug()<<sql2<<endl;
        if(model_offset->lastError().isValid())
        {
            alert(model_offset->lastError().text());
            return ;
        }
        model_offset->setQuery("select * from offset_test", db);
        alert(tr("更新成功"));
}


void Dialog::init_email()
{
    QString server = ui->server->text().trimmed();
    QString port = ui->port->text().trimmed();
    QString uname = ui->uname->text().trimmed();
    QString passwd = ui->paswd->text().trimmed();
    QString rcpt = ui->rcpt->text().trimmed();
    QString title = ui->subject->text().trimmed();

    QString sql = tr("select * from email_server where id = 1");
    model_email->setQuery(sql, db);
    if(model_email->lastError().isValid())
    {
        alert(model_email->lastError().text());
        return ;
    }

    ui->server->setText(model_email->data(model_email->index(0, 1)).toString());
    ui->port->setText(model_email->data(model_email->index(0, 2)).toString());
    ui->uname->setText(model_email->data(model_email->index(0, 3)).toString());
    ui->paswd->setText(model_email->data(model_email->index(0, 4)).toString());
    ui->rcpt->setText(model_email->data(model_email->index(0, 5)).toString());
    ui->subject->setText(model_email->data(model_email->index(0, 6)).toString());

}

// 邮个发送设置

void Dialog::on_pushButton_12_clicked()
{
   QString server = ui->server->text().trimmed();
   QString port = ui->port->text().trimmed();
   QString uname = ui->uname->text().trimmed();
   QString passwd = ui->paswd->text().trimmed();
   QString rcpt = ui->rcpt->text().trimmed();
   QString title = ui->subject->text().trimmed();

   QString sql = tr("update email_server set "
                    " smtp_server = '%1', "
                    " port = '%2', "
                    " user = '%3', "
                    " password = '%4', "
                    " email = '%5', "
                    " title = '%6' "
                    " where id = 1 "
                    ).arg(server).arg(port).arg(uname).arg(passwd).arg(rcpt).arg(title);

   model_email->setQuery(sql, db);

   if(model_email->lastError().isValid())
   {
       qDebug()<<sql<<endl;
       alert(model_email->lastError().text());
       return ;
   }
   model_email->setQuery("select * from email_server", db);



}


void Dialog::on_pushButton_14_clicked()
{
    QWebView *view = new QWebView(this);
    view->load(QUrl("http://localhost:8000/report"));

    QString fileName = QFileDialog::getSaveFileName(this,"Export PDF",QString(),"*.pdf");
    if(!fileName.isEmpty())
    {
      if(QFileInfo(fileName).suffix().isEmpty())
      {
          fileName.append(".pdf");
       }
       QPrinter  printer(QPrinter::HighResolution);
       printer.setOutputFormat(QPrinter::PdfFormat);

       printer.setPaperSize(QPrinter::A4);
       printer.setOutputFileName(fileName);
//       printer.setOutputFileName(tr("c:\\a.pdf"));

       view->print(&printer);
    }
}

void Dialog::init_about()
{

}
